package org.noka.tree;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.password.AESPlus;
import org.nokatag.system.BugInfoWrint;
import org.nokatag.system.Concest;
import org.nokatag.system.Function;
import org.nokatag.tagjava.NokaTag;
/**
 * 树形下拉选择
 * @author rebin
 * @2014-01-14
 */
public class TreeAjaxSelect extends  NokaTag{

	private static final long serialVersionUID = 7550610672397733696L;
	private  JspWriter jspOut =null;
	private String id = null; //id
	private String sql = null;//sql
	private String url = null;//url
	private String target = null;//target
	private String formatcell=null;//格式化列
	private String pramevar = null;//参数
	private String spid = null;//上级节点
	private String level= "2";//预加载级数，0为全部加载
	private String titleonclick=null;//标题单击时调用
	private String nodeonclick=null;//节点单击时调用
	private String name =null;//控件名字
	private String checktype=null;//级联选择关系  all:全部级联,next向下级联,previous向上，nomuch不级联
	private String checkboxonclick=null;//选择框点击事件
	private Boolean titlecliek=true;//节点标题是否响应点击事件
	private String value=null;//初始数据
	private String width="120";//宽度
	private String readonly=null;//只读
	private String disabled = null;
	private String checkname=null;
	private String allownull="true";
	private String placeholder = null;
	private String lower = null;//下级选择框的id
	private String titllev = null;//长标题的级数，默认为0，返回当前级别,-1返回有级
	private String height = "300";//下拉框最大高度
	private String dataurl = null;//数据加载url
	
	public int doStartTag()throws JspException{
		try{
			jspOut= pageContext.getOut();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			//----------参数处理----------------------------------
			if(null==sql && null!=dataurl){
				sql = "test";
			}
			String sql_s = AESPlus.encrypt(sql, Concest.SQLKEY);//加密sql
			String url_s = (url==null?"":AESPlus.encrypt(url, Concest.SQLKEY));//加密url
			String fc =(formatcell==null?"":AESPlus.encrypt(formatcell, Concest.SQLKEY));//加密formatcell
			String pr =(pramevar==null?"":AESPlus.encrypt(pramevar, Concest.SQLKEY));//加密formatcell
			String tr=(target==null?"":target);
			String sid=(spid==null?"":spid);
			String hei=LenNumber(height);
			String basePath = basepath(request);
			StringBuilder body = new StringBuilder();
			body.append(potjs(request));//js框架
			body.append(treejs(request));//tree js
			FormHtmlInsertid(getId(),getName(),getValue());
			body.append("<div id=\""+id+"_bdiv\">");
			body.append("<div id=\""+id+"_sdiv\" class=\"dtree\" style=\"float:left;padding:0px;margin:0px;\"></div>");
			body.append("<div id=\""+id+"_tdiv\" class=\"dtree\" style=\"z-index: 999;background-color: #fff;display:none;position:absolute;overflow:auto;top:0px;left:0px;border: 1px solid #ddd;\"></div>");
			body.append("</div>");
			body.append("<script type=\"text/javascript\">");
			//---------------显示树形菜单---------------------------
			body.append("Event.observe(document, 'dom:loaded', function() {");
			body.append("var cfg"+id+" = {");
			body.append("id:'"+id+"',");//id
			body.append("rurl:'"+basePath+"',");//根url
			body.append("slang:'"+Function.lung(request,"org_nokatag_select_pleaseselect")+"',");//语言
			body.append("url:'"+url_s+"',");//url
			body.append("level:"+level+",");//预加载级别
			body.append("spid:'"+sid+"',");//父id
			body.append("sql:'"+sql_s+"',");//sql
			body.append("pramevar:'"+pr+"',");//参数
			body.append("target:'"+tr+"',");//目标
			body.append("formatcell:'"+fc+"',");//格式化列
			body.append("inputname:'"+(name==null?id:name)+"',");//控件名字
			body.append("checkname:'"+(checkname==null?id+"_check":checkname)+"',");//控件名字
			body.append("value:'"+(value==null?"":value)+"',");//控件初始值
			body.append("readonly:'"+(readonly==null?"":readonly)+"',");//只读
			body.append("disabled:'"+(disabled==null?"":disabled)+"',");
			body.append("titlecliek:"+titlecliek+",");//标题是否响应点击事件
			body.append("allownull:"+allownull+",");//允许为空
			body.append("placeholder:'"+(placeholder==null?"":placeholder)+"',");
			body.append("lower:'"+(lower==null?"":lower)+"',");
			if(null!=titllev){
				body.append("titllev:"+titllev+",");
			}
			if(null!=dataurl){
				body.append("dataurl:'"+dataurl+"',");
			}
			body.append("width:"+LenNumber(width)+",");//格式化列
			body.append("height:"+hei+",");//下拉框最大高度
			body.append("checktype:'"+(checktype==null?"":checktype)+"',");//级联关系
			body.append("titleonclick:function(self){"+NJSFunction(titleonclick)+"},");//单击标题
			body.append("nodeonclick:function(self){"+NJSFunction(nodeonclick)+"},");//单击节点
			body.append("checkboxonclick:function(self){"+JSFunction(checkboxonclick)+"}");//单击勾选框
			body.append("};");
			body.append("var nr"+id+" = new ntree(cfg"+id+");");
			body.append("nr"+id+".selectShow();");
			body.append("});");
			body.append("</script>");
		
			jspOut.write(body.toString());
					
		}catch(Exception se){
			 BugInfoWrint.Print(se);
		}
		return EVAL_BODY_INCLUDE;
	}
	public JspWriter getJspOut() {
		return jspOut;
	}
	public void setJspOut(JspWriter jspOut) {
		this.jspOut = jspOut;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	
	public String getFormatcell() {
		return formatcell;
	}
	public void setFormatcell(String formatcell) {
		this.formatcell = formatcell;
	}
	public String getPramevar() {
		return pramevar;
	}
	public void setPramevar(String pramevar) {
		this.pramevar = pramevar;
	}
	public String getSpid() {
		return spid;
	}
	public void setSpid(String spid) {
		this.spid = spid;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public String getTitleonclick() {
		return titleonclick;
	}
	public void setTitleonclick(String titleonclick) {
		this.titleonclick = titleonclick;
	}
	public String getNodeonclick() {
		return nodeonclick;
	}
	public void setNodeonclick(String nodeonclick) {
		this.nodeonclick = nodeonclick;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getChecktype() {
		return checktype;
	}
	public void setChecktype(String checktype) {
		this.checktype = checktype;
	}
	public String getCheckboxonclick() {
		return checkboxonclick;
	}
	public void setCheckboxonclick(String checkboxonclick) {
		this.checkboxonclick = checkboxonclick;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getReadonly() {
		return readonly;
	}
	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}
	public String getCheckname() {
		return checkname;
	}
	public void setCheckname(String checkname) {
		this.checkname = checkname;
	}
	public String getAllownull() {
		return allownull;
	}
	public void setAllownull(String allownull) {
		this.allownull = allownull;
	}
	public Boolean getTitlecliek() {
		return titlecliek;
	}
	public void setTitlecliek(Boolean titlecliek) {
		this.titlecliek = titlecliek;
	}
	public String getPlaceholder() {
		return placeholder;
	}
	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}
	public String getLower() {
		return lower;
	}
	public void setLower(String lower) {
		this.lower = lower;
	}
	public String getTitllev() {
		return titllev;
	}
	public void setTitllev(String titllev) {
		this.titllev = titllev;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getDisabled() {
		return disabled;
	}
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}
	public String getDataurl() {
		return dataurl;
	}
	public void setDataurl(String dataurl) {
		this.dataurl = dataurl;
	}
}
